package ca.uvic.cs.cogz;

import java.awt.Dimension;

import javax.swing.JComponent;

import ca.uvic.cs.cogz.mapping.SuggestionsManager;
import ca.uvic.cs.cogz.mapping.TemporaryMappingManager;
import ca.uvic.cs.cogz.mapping.ontology.DataLoaderFacade;
import ca.uvic.cs.cogz.mapping.ui.MappingVisualizationPanel;
import ca.uvic.cs.cogz.stats.StatsManager;
import edu.stanford.smi.protege.model.KnowledgeBase;
import edu.stanford.smi.protegex.prompt.plugin.model.MapUIPluginPerspective;
import edu.stanford.smi.protegex.prompt.plugin.util.KnowledgeBaseFacade;

/**
 * The main UI plugin entry point. All UI extensions for the Prompt mapping UI are managed within
 * this class.
 * 
 * @author seanf
 */
public class PromptUIPlugin implements MapUIPluginPerspective {
	private SuggestionsManager suggestionsManager;
	private TemporaryMappingManager temporaryMappingManager;

	private StatsManager sourceStatsManager;
	private StatsManager targetStatsManager;

	private MappingVisualizationPanel mappingPanel;

	private static Dimension size;

	public PromptUIPlugin() {}

	public static void main(String[] args) {
		edu.stanford.smi.protegex.prompt.PromptTab.main(args);
	}

	public static Dimension getSize() {
		return size;
	}

	/**
	 * Called after the Prompt mapping UI has loaded. In this method, the CogZ UI extensions are
	 * created and added to Prompt through Prompt's plugin API.
	 */
	public void afterLoad() {
		this.temporaryMappingManager = new TemporaryMappingManager();
		this.suggestionsManager = new SuggestionsManager();
		this.sourceStatsManager = new StatsManager(suggestionsManager, StatsManager.SOURCE);
		this.targetStatsManager = new StatsManager(suggestionsManager, StatsManager.TARGET);

		// create the filter and add it as a header button and selection listener
		/*FilterPromptAction promptFilter = new FilterPromptAction("Filters", temporaryMappingManager);

		JTabbedPane sourcePanel = getTabPanel(promptFilter, KnowledgeBaseFacade.getMappingSourceKnowledgeBaseInMerging(), sourceStatsManager, StatsManager.SOURCE);
		JTabbedPane targetPanel = getTabPanel(promptFilter, KnowledgeBaseFacade.getMappingTargetKnowledgeBaseInMerging(), targetStatsManager, StatsManager.TARGET);

		MappingTabComponent.addLeftScreenPanel(sourcePanel);
		MappingTabComponent.addRightScreenPanel(targetPanel);

		SelectableContainer container = MapPluginFacade.getSelectableContainer(MapPluginFacade.UI_UTILITY_SUGGESTION);
		container.addSelectionListener(new SuggestionListListener(sourceViewer, targetViewer));
		container.addSelectionListener(promptFilter);

		// add CogZ button extensions
		container.addHeaderButton(new MarkAsTemporaryAction(temporaryMappingManager));
		container.addHeaderButton(new ViewMappingsAction(temporaryMappingManager, suggestionsManager, sourceStatsManager, targetStatsManager));
		container.addHeaderButton(promptFilter);*/
	}

	public void beforeClose() {
		mappingPanel.dispose();
	}

	public String getPluginName() {
		return "CogZ";
	}

	public void invokePlugin() {}

	public JComponent createContentPane(Dimension size) {
		PromptUIPlugin.size = size;

		KnowledgeBase sourceKb = KnowledgeBaseFacade.getMappingSourceKnowledgeBaseInMerging().getKnowledgeBase();
		KnowledgeBase targetKb = KnowledgeBaseFacade.getMappingTargetKnowledgeBaseInMerging().getKnowledgeBase();
		KnowledgeBase mappingKb = KnowledgeBaseFacade.getMappingKnowledgeBase().getProject().getKnowledgeBase();

		mappingPanel = DataLoaderFacade.getInstance().loadMapping(sourceKb, targetKb, mappingKb, temporaryMappingManager, suggestionsManager, sourceStatsManager, targetStatsManager);

		return mappingPanel;
	}
}
